package com.by.component.contract;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.by.component.contract.dto.*;
import com.by.component.contract.mapper.ContractInfoMapper;
import com.by.datasource.utils.PageUtil;
import com.by.toolkit.enums.FlagEnum;
import com.by.toolkit.model.PageReq;
import com.by.toolkit.model.PageResp;
import com.by.toolkit.utils.ByIfNotEmptyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.by.toolkit.utils.ExceptionUtil.nullError;


@Slf4j
@DubboService
public class ContractInfoServiceImpl implements ContractInfoService {

    @Resource
    private ContractInfoMapper contractInfoMapper;

    @Override
    public void saveContractInfo(AddContractSDTO dto) {
        contractInfoMapper.insert(ContractInfo.builder()
                .contractName(dto.getContractName())
                .contractType(dto.getContractType())
                .companyName(dto.getCompanyName())
                .verifyPhoneFlag(dto.getVerifyPhoneFlag())
                .verifiedFlag(dto.getVerifiedFlag())
                .officialSealId(dto.getOfficialSealId())
                .signContentId(dto.getSignContentId())
                .build());
    }

    @Override
    public void editContractInfo(EditContractSDTO dto) {
        contractInfoMapper.updateById(ContractInfo.builder()
                .id(dto.getId())
                .contractName(dto.getContractName())
                .contractType(dto.getContractType())
                .companyName(dto.getCompanyName())
                .verifyPhoneFlag(dto.getVerifyPhoneFlag())
                .verifiedFlag(dto.getVerifiedFlag())
                .officialSealId(dto.getOfficialSealId())
                .signContentId(dto.getSignContentId())
                .build());
    }

    @Override
    public ContractInfo4AdminSDTO contractInfo4Admin(Long contractInfoId) {
        ContractInfo contractInfo = contractInfoMapper.selectById(contractInfoId);
        nullError(contractInfo, "合同信息不存在");
        return ContractInfo4AdminSDTO.builder()
                .id(contractInfo.getId())
                .contractName(contractInfo.getContractName())
                .contractType(contractInfo.getContractType())
                .companyName(contractInfo.getCompanyName())
                .verifyPhoneFlag(contractInfo.getVerifyPhoneFlag())
                .verifiedFlag(contractInfo.getVerifiedFlag())
                .officialSealId(contractInfo.getOfficialSealId())
                .signContentId(contractInfo.getSignContentId())
                .build();
    }

    @Override
    public PageResp<ContractInfo4PageSDTO> queryPage(PageReq<ContractInfo4PageSDTO> req) {
        return PageUtil.toPageResp(contractInfoMapper.queryPage(Page.of(req.getPageIdx(), req.getPageSize()), req.getCondition()));
    }

    @Override
    public boolean verifyUserFlag(Long contractInfoId) {
        ContractInfo contractInfo = contractInfoMapper.selectOne(new LambdaQueryWrapper<ContractInfo>()
                .select(ContractInfo::getVerifiedFlag)
                .eq(ContractInfo::getId, contractInfoId));
        nullError(contractInfo, "合同信息不存在");
        return FlagEnum.resolve(contractInfo.getVerifiedFlag());
    }

    @Override
    public ContractInfo4DeliverSDTO queryContractInfo4Deliver(Long contractSignRecordId) {
        return contractInfoMapper.queryContractInfo4Deliver(contractSignRecordId);
    }

    @Override
    public Map<Long, String> contractNameMap(List<Long> contractIds) {
        if (CollectionUtils.isEmpty(contractIds)) {
            return Collections.emptyMap();
        }
        List<ContractInfo> contractInfos = contractInfoMapper.selectList(Wrappers.lambdaQuery(ContractInfo.class)
                .select(ContractInfo::getId,
                        ContractInfo::getContractName)
                .in(ContractInfo::getId, contractIds));
        return ByIfNotEmptyUtil.getIfNotEmpty(contractInfos
                , () -> contractInfos.stream().collect(Collectors.toMap(ContractInfo::getId, ContractInfo::getContractName))
                , Collections.emptyMap());
    }
}
